//
//  OrderCell.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/19.
//

#import "OrderCell.h"
#import <SDWebImage/SDWebImage.h>
@interface OrderCell()
@property(nonatomic,strong)UILabel *mLabel;
@property(nonatomic,strong)UILabel *Label1;
@property(nonatomic,strong)UILabel *Label2;
@property(nonatomic,strong)UILabel *Label3;
@property(nonatomic,strong)UILabel *tLabel1;
@property(nonatomic,strong)UILabel *tLabel2;
@property(nonatomic,strong)UILabel *tLabel3;

@property(nonatomic,strong)UIImageView *logo;
@property(nonatomic,strong)UILabel *name;
@property(nonatomic,strong)UILabel *typeT;
@end
@implementation OrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return  self;
}

-(void)setSource:(NSDictionary *)source
{
    self.mLabel.text = CPS(source[@"frictional"]);
    NSArray *items = source[@"mycar"];
    [items enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            _tLabel1.text = obj[@"supposing"];
            _Label1.text = obj[@"driven"];
        }else if (idx == 1){
            _tLabel2.text = obj[@"supposing"];
            _Label2.text = obj[@"driven"];
        }else if (idx == 2){
            _tLabel3.text = obj[@"supposing"];
            _Label3.text = obj[@"driven"];
        }
    }];
    if (![CPS(source[@"mill"]) isEqualToString:@""]) {
        [_agree setTitle:CPS(source[@"mill"]) forState:UIControlStateNormal];
        [self.agree setHidden:NO];
    }else{
        [self.agree setHidden:YES];
    }
    [_logo sd_setImageWithURL:[NSURL URLWithString:CPS(source[@"sorrow"])] placeholderImage:[UIImage imageNamed:@"ddlogo"]];
    _name.text = CPS(source[@"continued"]);
    _typeT.text = CPS(source[@"deckwhat"]);
}

-(void)setupUI{
    UIView *view = [UIView new];
    view.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(20);
        make.left.mas_offset(13);
        make.right.mas_offset(-13);
        make.bottom.mas_offset(0);
    }];
    UIImageView *bg = [UIImageView new];
    bg.image = [UIImage imageNamed:@"ddBG"];
    [view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_offset(0);
        make.height.mas_equalTo(132);
    }];
    
    UIImageView *icon = [UIImageView new];
    icon.image = [UIImage imageNamed:@"ddlogo"];
    icon.layer.cornerRadius = 5;
    icon.layer.masksToBounds = YES;
    [view addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(8);
        make.left.mas_offset(12);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    _logo = icon;
    
    UILabel *name = [UILabel new];
    name = [UILabel new];
    name.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
    name.textColor = HexColor(@"#0B9700");
    _name = name;
    [view addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(icon);
        make.left.equalTo(icon.mas_right).offset(5);
    }];
    
    [view addSubview:self.agree];
    [_agree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);;
        make.centerY.mas_equalTo(icon);
    }];
    [self setButtonLeftTextRightImage:_agree];
    UILabel *label = [UILabel new];
    label.text = @"";
    label.textColor = HexColor(@"#000000");
    label.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view);
        make.left.mas_offset(18);
    }];
    [view addSubview:self.mLabel];
    [_mLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(2);
        make.bottom.mas_equalTo(label);
    }];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view);
        make.right.mas_offset(-15);
    }];
    UILabel *t1 = [UILabel new];
    t1.textColor = DetailColor;
    t1.font = [UIFont systemFontOfSize:12];
    [view addSubview:t1];
    [t1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(17);
        make.bottom.mas_offset(-26);
    }];
    [view addSubview:self.Label1];
    [_Label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(t1.mas_bottom).offset(2);
        make.centerX.mas_equalTo(t1);
    }];
    
    
    UILabel *t2 = [UILabel new];
    t2.textColor = DetailColor;
    t2.font = [UIFont systemFontOfSize:12];
    [view addSubview:t2];
    [t2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view);
        make.bottom.mas_offset(-26);
    }];
    [view addSubview:self.Label2];
    [_Label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(t2.mas_bottom).offset(2);
        make.centerX.mas_equalTo(t2);
    }];

    
    UILabel *t3 = [UILabel new];
    t3.textColor = DetailColor;
    t3.font = [UIFont systemFontOfSize:12];
    [view addSubview:t3];
    [t3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-24);
        make.bottom.mas_offset(-26);
    }];
    [view addSubview:self.Label3];
    [_Label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(t3.mas_bottom).offset(2);
        make.centerX.mas_equalTo(t3);
    }];

    [view addSubview:self.check];
    _tLabel1 = t1;
    _tLabel2 = t2;
    _tLabel3 = t3;
    [_check mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.mas_equalTo(view);
        make.size.mas_equalTo(CGSizeMake(103, 34));
    }];
    [_check addSubview:self.typeT];
    [_typeT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(_check);
    }];
}
- (void)setButtonLeftTextRightImage:(UIButton *)button {
    // 获取文字和图片的宽度
    CGFloat titleWidth = button.titleLabel.intrinsicContentSize.width;
    CGFloat imageWidth = button.imageView.frame.size.width;
    // 设置文字和图片的间距
    CGFloat spacing = 10;
    // 调整文字和图片的布局
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth + spacing);
    button.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth + spacing +100, 0, -titleWidth);
}
-(UIButton *)agree
{
    if(!_agree){
        _agree = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_agree setTitle:@"Loan agreement" forState:UIControlStateNormal];
        [_agree setTitleColor:HexColor(@"#FF7F03") forState:UIControlStateNormal];
        _agree.titleLabel.font = [UIFont systemFontOfSize:13];
        [_agree setImage:[UIImage imageNamed:@"ddturn"] forState:UIControlStateNormal];
    }
    return _agree;
}
-(UILabel *)mLabel
{
    if (!_mLabel) {
        _mLabel = [UILabel new];
//        _mLabel.text = @"80,000";
        _mLabel.textColor = HexColor(@"#000000");
        _mLabel.font = [UIFont systemFontOfSize:28 weight:UIFontWeightBold];
    }
    return _mLabel;
}

-(UILabel *)Label1
{
    if (!_Label1) {
        _Label1 = [UILabel new];
        _Label1.text = @"";
        _Label1.textColor = HexColor(@"#000000");
        _Label1.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    }
    return _Label1;
}

-(UILabel *)Label2
{
    if (!_Label2) {
        _Label2 = [UILabel new];
        _Label2.text = @"";
        _Label2.textColor = HexColor(@"#000000");
        _Label2.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    }
    return _Label2;
}

-(UILabel *)Label3
{
    if (!_Label3) {
        _Label3 = [UILabel new];
        _Label3.text = @"";
        _Label3.textColor = HexColor(@"#000000");
        _Label3.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    }
    return _Label3;
}

-(UIView *)check
{
    if (!_check) {
        _check = [UIView new];
        _check.backgroundColor = HexColor(@"#FF9E01");
        _check.layer.cornerRadius = 17;
    }
    return _check;
}
-(UILabel *)typeT
{
    if (!_typeT) {
        _typeT = [UILabel new];
        _typeT.textColor = HexColor(@"#ffffff");
        _typeT.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    }
    return _typeT;
}
@end
